package com.dragon.web.commons.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.marvels.common.utils.MarvelsLogUtil;

import net.coobird.thumbnailator.Thumbnails;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 
 * 图片的工具类
 * @author ljs
 *
 */
public class ImageUtils {
	
	public static void main(String[] args) {
		//File file = new File("C:/Users/IT/Desktop/big.png");
		File file = new File("E:\\longsail\\aaa.png");
		
		//File file = new File("D:\\dev\\tomcat7\\wtpwebapps\\fintech-api-web\\upload/2017/3/4/idCard_1488592746105.png");
		thumbnailImage(file,"small_");
	}
	
	/**
	 * 把base64的图片字符串转换成图片
	 * @param imgStr
	 * @return
	 */
	@SuppressWarnings("restriction")
	public static void base64ToImg(String imgStr, File imgFile) {
		if (imgStr == null) { return;}
		if(!imgFile.getParentFile().exists()){
			imgFile.getParentFile().mkdirs();
		}
		BASE64Decoder decoder = new BASE64Decoder();
		OutputStream out = null;
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			out = new FileOutputStream(imgFile);
			out.write(b);
			out.flush();
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====ERROR=====", e);
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				MarvelsLogUtil.getInstance().error("=====ERROR=====", e);
			}
		}
	}
	
	public static String imageToBase64(File file){
        InputStream in = null;  
        byte[] data = null;  
        //读取图片字节数组  
        try {  
            in = new FileInputStream(file);          
            data = new byte[in.available()];  
            in.read(data);  
        } catch (IOException e){  
            MarvelsLogUtil.getInstance().error("imageToBase64=====ERROR=====", e);
        } finally {
        	try {
        		if(in != null){
					in.close();
        		}
			} catch (IOException e) {
				MarvelsLogUtil.getInstance().error("imageToBase64=====ERROR=====", e);
			}  
		}
        
       //返回Base64编码过的字节数组字符串  
        return imageToBase64(data);
    }  
	
	
	/**
	 *  //图片转化成base64字符串  
	 *	//将图片文件转化为字节数组字符串，并对其进行Base64编码处理  
	 * @param imgFile
	 * @return
	 */
	public static String imageToBase64(String imgFile){
    	return imageToBase64(new File(imgFile));
    }  
    
    @SuppressWarnings("restriction")
    public static String imageToBase64(byte[] data) {
    	if(null == data || data.length == 0) {
    		return null;
    	}
    	//对字节数组Base64编码  
        BASE64Encoder  encoder = new BASE64Encoder ();  
        return encoder.encode(data);//返回Base64编码过的字节数组字符串  
    }
	
	/**
     * <p>Title: thumbnailImage</p>
     * <p>Description: 根据图片路径生成缩略图 </p>
     * @param imagePath    原图片路径
     * @param w            缩略图宽
     * @param h            缩略图高
     * @param prevfix    生成缩略图的前缀
     * @param force        是否强制按照宽高生成缩略图(如果为false，则生成最佳比例缩略图)
     */
    public static void thumbnailImage(File imgFile,String samllImageFilePath){
    	if(!imgFile.exists()){
    		MarvelsLogUtil.getInstance().warn("ImageUtils.thumbnailImage the image is not exist.");
    		return;
    	}
    	try {
			Thumbnails.of(imgFile).scale(0.7f).outputQuality(0.25f).toFile(new File(samllImageFilePath));
		} catch (IOException e) {
			MarvelsLogUtil.getInstance().error("ImageUtils.thumbnailImage errir",e);
		}
    }
}
